package org.southyin.securitysamples.controller;

import io.swagger.annotations.*;
import io.swagger.v3.oas.annotations.Operation;
import org.southyin.securitysamples.commons.CommonsResp;
import org.southyin.securitysamples.model.Category;
import org.southyin.securitysamples.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/category")
@Api(tags = "CategoryController",description = "栏目管理接口")
public class CategoryController {
    @Autowired
    CategoryService categoryService;

    @ApiOperation(value = "新增接口",notes = "新增栏目，栏目名称不能为空")
    @PostMapping("/")
    public CommonsResp insert(@RequestBody Category category) {
        if ("".equals(category.getCname().trim())) {
            CommonsResp.failed(500, "栏目名称不能为空");
        }
        return categoryService.insertCategory(category);
    }

//    @ApiOperation(value = "删除接口",notes = "根据id删除栏目")
    @Operation(summary = "删除接口",description = "根据id删除栏目")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "path",name = "栏目id",required = true)
    })
    @DeleteMapping("/{id}")
    public CommonsResp delById(@PathVariable @ApiParam("栏目id") long id) {
        return categoryService.delCategoryById(id);
    }

    @ApiOperation(value = "更新接口",notes = "根据id修改栏目名称")
    @PutMapping("/")
    public CommonsResp updateCategoryById(@RequestBody Category category) {
        return categoryService.updateCategoryById(category);
    }

    @ApiOperation(value = "查询接口",notes = "根据查询关键字查询栏目列表")
    @GetMapping("/")
    public List<Category> listCategories(String keyWords) {
        return categoryService.listCategories(keyWords);
    }

    @ApiOperation(value = "批量删除接口",notes = "根据ids删除栏目")
    @DeleteMapping("/multiDel")
    public CommonsResp multiDel(long[] ids) {
        return categoryService.multiDel(ids);
    }

    @ApiOperation(value = "更新栏目状态",notes = "根据id修改栏目状态")
    @PutMapping("/updateCategoryDelMark")
    public CommonsResp updateCategoryDelMark(@RequestBody Category category) {
        return categoryService.updateCategoryDelMark(category);
    }

    @ApiOperation(value = "查询有效类别接口",notes = "查询有效类别接口")
    @GetMapping("/listEnabledCategory")
    public List<Category> listEnabledCategory() {
        return categoryService.listEnabledCategory();
    }
}
